package com.spirng.leetcode.day07;

import java.awt.*;
import java.util.ArrayList;
import java.util.HashMap;

public class Test105 {
    public static void main(String[] args) {
        ArrayList<Integer> list = new ArrayList<>();
        list.add(1);
        for (Integer i : list) {
            list.remove(i);
        }
    }
    HashMap<Integer,Integer> map=new HashMap<>();
    public TreeNode buildTree(int[] preorder, int[] inorder) {
        for (int i = 0; i < inorder.length; i++) {
            map.put(inorder[i],i);
        }
        return findNode(preorder,0,preorder.length-1,inorder,0,inorder.length-1);
    }
    public TreeNode findNode(int [] preorder,int preBegin,int preEnd,int []inorder,int inBegin,int inEnd){
        if (preBegin>preEnd||inBegin>inEnd){
            return null;
        }
        if(preBegin==preorder.length){
            return new TreeNode(preorder[preBegin]);
        }
        int val = preorder[preBegin];
        Integer rootNode = map.get(val);
        int size=rootNode- inBegin;//左右节点个数
        TreeNode treeNode=new TreeNode(val);
        treeNode.left=findNode(preorder,preBegin+1,preBegin+size,inorder,inBegin,inBegin+size-1);
        treeNode.right=findNode(preorder,preBegin+size+1,preEnd,inorder,rootNode+1,inEnd);
        return treeNode;
    }
}
